package top.chatgqt.SmartChargeStation.demo.mqtt.PahoMode.utils;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;
import top.chatgqt.SmartChargeStation.demo.mqtt.PahoMode.conf.MqttConf;
import top.chatgqt.SmartChargeStation.demo.mqtt.PahoMode.service.impl.Callback;
import top.chatgqt.SmartChargeStation.demo.mqtt.PahoMode.service.impl.MessageListener;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class MqttClientUtils {

    @Resource
    private MqttConf conf;

    // 监听类 监听订阅的消息
    @Resource
    private MessageListener messageListener;

    private MqttClient client;
    private MqttConnectOptions options;

    @PostConstruct
    public void init() {
//        createMqttClient();
//        createMqttConnectOptions();
//        connect();

//        subScribe("io_test");
    }

    // 连接
    private void connect() {

        try {
            if (client.isConnected()) return;

            client.connect(options);
            // 设置回调类
            client.setCallback(new Callback());
            log.info(" >>>>>>> mqtt client connect success ｜｜ mqtt客户端连接成功");
        } catch (MqttException e) {
            log.error(" >>>>>>> mqtt client connect fail ｜｜ mqtt客户端连接失败");
            throw new RuntimeException(e);
        }
    }

    /**
     * 发送
     */
    public void publish(String topic, String message) {

        MqttMessage mqttMessage = new MqttMessage(message.getBytes(StandardCharsets.UTF_8));

        try {
            client.publish(topic, mqttMessage);
            log.info(" >>>>>>> mqtt client publish ｜｜ 客户端推送成功");
        } catch (MqttException e) {
            log.error(" >>>>>>> mqtt client publish fail ｜｜ 客户端推送失败");
            throw new RuntimeException(e);
        }
    }


    /**
     * 订阅
     */
    public void subScribe(String topic) {

        try {
            client.subscribe(topic, messageListener);
            log.info(" >>>>>>> mqtt client subscribe ｜｜ 客户端订阅成功");
        } catch (MqttException e) {
            log.error(" >>>>>>> mqtt client subscribe fail ｜｜ 客户端订阅失败");
            throw new RuntimeException(e);
        }
    }

    /**
     * 断开连接
     */
    public void disconnect() {
        try {
            client.disconnect();
            log.info(" >>>>>>> mqtt client disconnect ｜｜ 客户端断开连接成功");
        } catch (MqttException e) {
            log.error(" >>>>>>> mqtt client disconnect fail ｜｜ 客户端断开连接失败");
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成配置对象
     */
    private void createMqttConnectOptions() {
        if (options != null) return;
        options = new MqttConnectOptions();
        options.setUserName(conf.getUsername());
        options.setPassword(conf.getPassword().toCharArray());
    }

    /**
     * 生成连接
     */
    private void createMqttClient() {
        if (client != null) return;
        try {
            client = new MqttClient(conf.getHost(), conf.getClientId());
            log.info(" >>>>>>> mqtt client 生成 <<<<<<<<<");
        } catch (MqttException e) {
            log.info(" >>>>>>> mqtt client 生成失败 <<<<<<<<<");
            throw new RuntimeException(e);
        }
    }
}
